package com.ac.api.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.ac.api.group.entity.AcGrouptype;
import com.ac.api.group.service.AcGrouptypeService;
import com.ac.api.group.vo.GroupTypeModuleVo;
import com.ac.api.group.vo.GroupTypeTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Api(tags = "角色类型")
@RestController
@RequestMapping("/acGrouptype")
public class AcGrouptypeController extends BaseController {

    private static final Logger             LOG = LoggerFactory.getLogger(AcGrouptypeController.class);
    @Autowired
    private              AcGrouptypeService entityService;

    @SysLog(action = "acGrouptype:findByPage", value = "角色管理：角色类型列表")
    @ApiOperation(value = "分页查询角色类型", notes = "分页查询角色类型")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcGrouptype>> findByPage(final AcGrouptype example, final Page page) {
        IPage<AcGrouptype> records = entityService.page(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "acGrouptype:delete", value = "角色管理：删除角色类型")
    @ApiOperation(value = "删除角色类型")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "角色类型ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除角色类型", null);
    }

    @SysLog(action = "acGrouptype:one", value = "角色管理：查询角色类型详情")
    @ApiOperation(value = "查询单个角色类型")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "角色类型ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcGrouptype entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "acGrouptype:add", value = "角色管理：添加角色类型")
    @ApiOperation(value = "添加角色类型")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcGrouptype entity) {
        entityService.save(entity);
        return new Result(true, "成功保存角色类型", null);
    }

    @SysLog(action = "acGrouptype:update", value = "角色管理：修改角色类型")
    @ApiOperation(value = "修改角色类型")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcGrouptype entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改角色类型", null);
    }

    @SysLog(action = "acGrouptype:all", value = "角色管理：全部角色类型")
    @ApiOperation(value = "全部角色类型")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcGrouptype>> all(AcGrouptype example) {
        //只允许获取管理组
        example.setId(1);
        List<AcGrouptype> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "acGrouptype:findByGrouptype", value = "角色管理：查询角色类型功能树")
    @ApiOperation(value = "查询角色类型功能树")
    @RequestMapping(value = "findByGrouptypeTree/{id}", method = RequestMethod.GET)
    public Result<GroupTypeTreeVo> findByGrouptypeTree(@PathVariable("id") final Integer id) {
        GroupTypeTreeVo tree = entityService.findByGrouptype(id);
        return new Result<>(true, tree);
    }

    @SysLog(action = "acGrouptype:findByUserMenu", value = "角色管理：查询角色显示菜单")
    @ApiOperation(value = "查询订单菜单")
    @RequestMapping(value = "findByUserMenu/{groupid}", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "groupid", value = "角色id", required = true, paramType = "path")})
    public Result<List<GroupTypeModuleVo>> findByUserMenu(@PathVariable("groupid") final Integer groupid) {
        List<GroupTypeModuleVo> modules = entityService.findGroupModule(groupid);
        return new Result<>(true, modules);
    }

}

